*****************************************************************
*****************************************************************
*** 0. General settings
*****************************************************************
*****************************************************************

* clear
clear
clear 		mata
mat 		drop _all
clear 		matrix

* set path
global		path	""				

set maxvar 32767







* Load data
	
use "$path/0 Data/_Processed/temp/Masterdata", clear

* Annual fees and credit line issuances by firm
keep if loantype2 == "Revolver"
collapse (mean) AISD AISU (sum) facilityamt, by(gvkey gvkey_lender year)		


save "$path/0 Data/_Processed/temp/cl_annual", replace



merge 	m:m	gvkey year using "$path/0 Data/_Processed/compustat-ratios"
drop	if _merge == 2
drop	_merge		 
  
merge 	m:m	gvkey year using "$path/0 Data/_Processed/temp/_equity_vola_annual" 
drop	if _merge == 2
drop	_merge		


merge 	m:m	gvkey year using "$path/0 Data/_Processed/US_Dealscan Firm_Betas_daily_annual"
drop	if _merge == 2
drop	_merge		


merge m:m gvkey_lender year using "$path/0 Data/_Processed/temp/_Bank"
drop	if _merge == 2
drop	_merge			
		
		
			
* Merge with srisk data
		
merge m:m ticker_lender year  using "$path/0 Data/_Processed/temp/srisk_2010_2019_annual"		
drop	if _merge == 2
drop	_merge			
	
	
merge m:m year using "$path/0 Data/_Processed/libor"
drop if _merge == 2
drop _merge

merge m:m year using "$path/0 Data/_Processed/vix_annual"
drop if _merge == 2
drop _merge

merge m:m gvkey_lender year using "$path/0 Data/_Processed/bank_betas"
drop if _merge == 2
drop _merge

	 
  gen perc_AISD = AISD / 100
gen perc_AISU = AISU / 100
gen srisk_ta = srisk*1000 / assets_total
  
  
 		gen beta_capital = w_beta * capital   
  gen lr_capital = liquidity_risk * capital   
  
  gen lrmes_capital = lrmes * capital
  
  gen srisk_capital = srisk_ta * capital
  gen vix_capital = vix * capital
  
 
gen AISUAISD_ratio = AISU/AISD 
  
  
* Winsorize data  
winsor perc_AISD, gen(w_perc_AISD) p(0.01)
winsor perc_AISU, gen(w_perc_AISU) p(0.01)
winsor AISUAISD_ratio, gen(w_AISUAISD_ratio) p(0.01)
winsor npl_loans, gen(w_npl_loans) p(0.01)
winsor roa, gen(w_roa) p(0.01)
winsor capital, gen(w_capital) p(0.01)
winsor log_assets, gen(w_log_assets) p(0.01)
winsor nonintinc, gen(w_nonintinc) p(0.01)
winsor lrmes, gen(w_lrmes) p(0.01)
winsor srisk_ta, gen(w_srisk_ta) p(0.01)
winsor liquidity_risk, gen(w_liquidity_risk) p(0.01)
  
  
* Lable variables
label var LIBOR "LIBOR"
label var w_npl_loans "NPL / Loans"
label var w_capital "Capital"
label var w_nonintinc "Non-Interest Income"
label var w_log_assets "Bank Size"
label var w_roa "Bank Profitability"
label var sd_equity_ann "Equity Volatility"
label var w_beta "Firm Equity Beta"
label var w_size "Firm Size"
label var w_profitability "Firm Profitability"
label var w_tobinq "Tobin's Q'"
label var w_leverage "Leverage"
label var w_tangibility "Tangibility"
label var w_bank_beta "Bank Equity Beta"
label var w_lrmes "LRMES"
label var w_srisk_ta "SRISK / Assets"
label var w_liquidity_risk "Liquidity Risk"






* Fee regressions  (Version 1)
eststo clear
			
eststo, title("Model 1"): quietly areg w_perc_AISD LIBOR  w_npl_loans w_capital  w_nonintinc w_log_assets w_roa  sd_equity_ann w_beta w_size w_profitability w_tangibility w_tobinq w_leverage i.fyear i.sic1, a(sic2) cluster(gvkey) 

eststo, title("Model 2"): quietly areg w_perc_AISD  w_bank_beta LIBOR  w_npl_loans w_capital  w_nonintinc w_log_assets w_roa  sd_equity_ann w_beta w_size w_profitability w_tangibility w_tobinq w_leverage i.fyear i.sic1, a(sic2) cluster(gvkey)

eststo, title("Model 3"): quietly areg w_perc_AISD  w_lrmes  LIBOR  w_npl_loans w_capital  w_nonintinc w_log_assets w_roa sd_equity_ann w_beta  w_size w_profitability w_tangibility w_tobinq w_leverage i.fyear i.sic1, a(sic2) cluster(gvkey)

eststo, title("Model 4"): quietly areg w_perc_AISD w_srisk_ta  LIBOR  w_npl_loans w_capital  w_nonintinc w_log_assets w_roa sd_equity_ann w_beta  w_size w_profitability w_tangibility w_tobinq w_leverage i.fyear i.sic1, a(sic2) cluster(gvkey)  
 
eststo, title("Model 5"): quietly areg w_perc_AISD w_liquidity_risk LIBOR  w_npl_loans w_capital  w_nonintinc w_log_assets w_roa sd_equity_ann w_beta  w_size w_profitability w_tangibility w_tobinq w_leverage i.fyear i.sic1, a(sic2) cluster(gvkey)  
   
  
eststo, title("Model 1"): quietly areg w_perc_AISU LIBOR  w_npl_loans w_capital  w_nonintinc w_log_assets w_roa  sd_equity_ann w_beta w_size w_profitability w_tangibility w_tobinq w_leverage i.fyear i.sic1, a(sic2) cluster(gvkey) 

eststo, title("Model 2"): quietly areg w_perc_AISU  w_bank_beta LIBOR  w_npl_loans w_capital  w_nonintinc w_log_assets w_roa  sd_equity_ann w_beta w_size w_profitability w_tangibility w_tobinq w_leverage i.fyear i.sic1, a(sic2) cluster(gvkey)

eststo, title("Model 3"): quietly areg w_perc_AISU  w_lrmes  LIBOR  w_npl_loans w_capital  w_nonintinc w_log_assets w_roa sd_equity_ann w_beta  w_size w_profitability w_tangibility w_tobinq w_leverage i.fyear i.sic1, a(sic2) cluster(gvkey)

eststo, title("Model 4"): quietly areg w_perc_AISU w_srisk_ta  LIBOR  w_npl_loans w_capital  w_nonintinc w_log_assets w_roa sd_equity_ann w_beta  w_size w_profitability w_tangibility w_tobinq w_leverage i.fyear i.sic1, a(sic2) cluster(gvkey)  
 
eststo, title("Model 5"): quietly areg w_perc_AISU w_liquidity_risk LIBOR  w_npl_loans w_capital  w_nonintinc w_log_assets w_roa sd_equity_ann w_beta  w_size w_profitability w_tangibility w_tobinq w_leverage i.fyear i.sic1, a(sic2) cluster(gvkey) 


  
esttab using "$path/04 Results/Table Fees-annual2.csv", p(3) label star(* 0.10 ** 0.05 *** 0.01) stats(r2_a  N, labels(R-squared "Number obs.")) order(w_beta w_bank_beta w_lrmes w_srisk_ta  w_liquidity_risk) replace	


